package com.xijia.wuyouwallet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.adapter.ProvinceAdapter;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.bean.ProvinceData;
import com.xijia.wuyouwallet.utils.Constants;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;
import com.xijia.wuyouwallet.widget.ErrorHintView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/7 0007.
 */

public class SelectProvinceActivity extends BaseActivity implements View.OnClickListener{

    private ImageView selectBank_back;
    private TextView selectBank_title;
    private ListView selectBank_lv;
    private ErrorHintView mErrorHintView;
    public static final int VIEW_CONTENT = 1;//view显示
    public static final int VIEW_WIFIFAILUER = 2;//显示断网
    public static final int VIEW_LOADFAILURE = 3;//加载失败
    public static final int VIEW_LOADING = 4;//加载
    public static final int VIEW_NODATA = 5;//无数据

    private List<ProvinceData.DataBean.ListBean> mList;
    private ProvinceAdapter mAdapter;

    private String bankCityName,bankCityCode,bankProvinceName,bankProvinceCode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_bank);
        initView();
    }

    private void initView(){
        selectBank_back = (ImageView) findViewById(R.id.selectBank_back);
        selectBank_back.setOnClickListener(this);
        selectBank_title = (TextView) findViewById(R.id.selectBank_title);
        selectBank_lv = (ListView) findViewById(R.id.selectBank_lv);
        mErrorHintView = (ErrorHintView) findViewById(R.id.mErrorHintView);
        selectBank_title.setText("选择省份");
        mList = new ArrayList<>();
        mAdapter = new ProvinceAdapter(mList,this);
        selectBank_lv.setAdapter(mAdapter);
        selectBank_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //Intent intent = new Intent();
                //intent.putExtra("bankProvince",mList.get(i).getCity_name());
                //intent.putExtra("bankProvinceCode",mList.get(i).getCity_code());
                //SelectProvinceActivity.this.setResult(Constants.RESULT_PROVINCE,intent);
                //SelectProvinceActivity.this.finish();
                Bundle bundle = new Bundle();
                bankProvinceName = mList.get(i).getCity_name();
                bankProvinceCode = mList.get(i).getCity_code();
                bundle.putString("bankProvinceCode",mList.get(i).getCity_code());
                jumpToPage(SelectCityActivity.class,bundle,true, Constants.REQUEST_DATA,false);
            }
        });
        requestData();
    }
    /**
     * 获取数据
     */
    private void requestData(){
        Map<String,Object> map = new HashMap<>();
        map.put("city_level","1");
        map.put("city_code","");
        XUtil.Post(UrlConfig.GET_CITY_LIST,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                //MyLogger.wLog().e("获取省份result="+result);
                ProvinceData data = JSONObject.parseObject(result,ProvinceData.class);
                if (data.getCode()==200){
                    for (ProvinceData.DataBean.ListBean listBean : data.getData().getList()){
                        mList.add(listBean);
                    }
                    mAdapter.notifyDataSetChanged();
                    showLoading(VIEW_CONTENT);
                }else {
                    showLoading(VIEW_NODATA);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                showLoading(VIEW_LOADFAILURE);
            }
        });
    }
    //显示对话框
    private void showLoading(int i) {
        selectBank_lv.setVisibility(View.GONE);
        mErrorHintView.setVisibility(View.GONE);
        switch (i) {
            case VIEW_CONTENT:
                mErrorHintView.hideLoading();
                selectBank_lv.setVisibility(View.VISIBLE);
                break;
            case VIEW_WIFIFAILUER:
                mErrorHintView.hideLoading();
                mErrorHintView.netError(new ErrorHintView.OperateListener() {
                    @Override
                    public void operate() {
                        showLoading(VIEW_LOADING);
                        //requestBanner();
                        //requestList(true, false, false, false, itemStateId, orderId, cycle_id);
                    }
                });
                break;
            case VIEW_LOADFAILURE:
                mErrorHintView.hideLoading();
                mErrorHintView.loadFailure(new ErrorHintView.OperateListener() {
                    @Override
                    public void operate() {
                        showLoading(VIEW_LOADING);
                        requestData();
                    }
                });
                break;
            case VIEW_NODATA:
                mErrorHintView.hideLoading();
                mErrorHintView.noData(new ErrorHintView.OperateListener() {
                    @Override
                    public void operate() {

                    }
                });
                break;
            case VIEW_LOADING:
                mErrorHintView.loadingData();
                break;
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.selectBank_back:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==Constants.REQUEST_DATA&&resultCode==Constants.RESULT_DATA){
            bankCityName = data.getStringExtra("bankCityName");
            bankCityCode = data.getStringExtra("bankCityCode");
            Intent intent = new Intent();
            intent.putExtra("bankCityName",bankCityName);
            intent.putExtra("bankProvinceName",bankProvinceName);
            intent.putExtra("bankCityCode",bankCityCode);
            intent.putExtra("bankProvinceCode",bankProvinceCode);
            SelectProvinceActivity.this.setResult(Constants.RESULT_PROVINCE,intent);
            SelectProvinceActivity.this.finish();
        }
    }
}
